//logs.js
//获取应用实例
const app = getApp()

Page({
  data: {
    singleId: '',
    singlePrice: '',
    paySatus: false,//支付状态
    explainInfo: []
  },
  onShow: function () {
    // 更新用户权限等级
    var wxUserId = app.globalData.wxUserId
    app.http.get('/wechat/getUserType?wxUserId=' + wxUserId).then(res =>{
      if(res.status == '200'){
        app.globalData.wxUserType = res.data
      }
    })
  },
  onLoad: function (option) {
    var that = this
    app.http.get('/wechat/getSinglePayInfo').then(res =>{
      if(res.status == '200'){
        var singleInfo = res.data.singleInfo
        var explainInfo = res.data.explainInfo
        that.setData({
          singlePrice: singleInfo.singlePrice,
          singleId: singleInfo.singleId,
          explainInfo: explainInfo
        })
      }
    })
  },
  singlePay: function(e) {
    var that = this
    if(!that.data.paySatus){
      var pay = e.currentTarget.dataset.single
      if(app.globalData.platform == "ios" && !app.globalData.iosPayStatus){
        wx.showToast({
          title: '由于相关规范，iOS功能暂不可用',
          icon: 'none',
          duration: 3000
        })
      }else {
        wx.showLoading()   
        that.payStartStatus()
        var wxUserId = app.globalData.wxUserId
        var session = app.globalData.session
        var data = {
          userId: wxUserId,
          openId: session.openid,
          goodsCodeValue: pay
        }
        app.http.post('/wechat/singleWxPay',data).then(res => {
          setTimeout(function () {
            wx.hideLoading()
            that.payEndStatus()
          },1 * 1000);
          if(res.status == '200'){
            var info = res.data
            that.doWxPay(info.singlePay, info.orderNo)
          } else {
            wx.showToast({
              title: res.msg,
              icon: "none"
            })
          }
        })
        // that.getPayStatus('66666666666')
      }
    }
  },
  goVip: function() {
    wx.navigateTo ({
      url: '../vip/vip'
    })
  },
  doWxPay(param, orderNo) { //拉起微信支付组件
    var that = this
    //小程序发起微信支付
    wx.requestPayment({
      timeStamp: param.timeStamp,//记住，这边的timeStamp一定要是字符串类型的，不然会报错
      nonceStr: param.nonce_str,
      package: param.packageStr,
      signType: param.signType,
      paySign: param.sign,
      success: function (event) {
        // 定时查询后台 是否支付完成。
        wx.showLoading()      
        that.getPayStatus(orderNo, 1)
      },
      fail: function (error) { //取消支付
        // fail
        wx.navigateTo({
          url: '/pages/payResult/payResult?type=2&pageType=1&orderNo=' + orderNo
        })
      }
    })
  },
  getPayStatus: function(orderNo, num) { // 获取微信支付状态
    var that = this
    console.log(orderNo + '---------' + num)
    if(num < 10) {
      num++
      app.http.get('/wechat/getOrderStatus?orderNo='+ orderNo).then(res => {
        if(res.status == '200'){
          if(res.data) {
            wx.hideLoading()
            wx.navigateTo({
              url: '/pages/payResult/payResult?type=1&pageType=1&orderNo=' + orderNo
            })
            // wx.showToast({
            //   title: '支付成功',
            //   icon: 'success',
            //   duration: 2000
            // });
            // setTimeout(() => {
              
            // }, 2000);   
          } else {
            setTimeout(that.getPayStatus(orderNo, num), 2000);
          }
        } else { // 再次请求
          setTimeout(that.getPayStatus(orderNo, num), 2000);
        }
      })
    } else { //查询订单信息超时
      wx.navigateTo({
        url: '/pages/payResult/payResult?type=2&pageType=1&orderNo=' + orderNo
      })
    }
  },
  payStartStatus:function() {
    var that = this
    that.setData({
      paySatus: true
    });
  },
  payEndStatus: function() {
    var that = this
    that.setData({
      paySatus: false
    });
  },
})
